//
//  MBProgressHUD+LZCategroy.m
//  LZBaseFrame
//
//  Created by 位立志 on 2018/3/12.
//  Copyright © 2018年 Bwton. All rights reserved.
//

#import "MBProgressHUD+DGCategroy.h"
#import <objc/runtime.h>
//#import "DGMacroDefine.h"


//  提示类型
typedef NS_ENUM(NSInteger, MBProgressHUDType){
    MBProgressHUDTypeLoading,                   //菊花
    MBProgressHUDTypeOnlyText,                  //文字中间
    MBProgressHUDTypeOnlyTextBottom,            //文字底部
    MBProgressHUDTypeSuccess,                   //成功
    MBProgressHUDTypeError,                     //失败
    MBProgressHUDTypeInfo,                      //配置
    MBProgressHUDTypeWarn,                      //警告
};

@implementation MBProgressHUD (DGCategroy)

static char *MBProgressHUDKey = "MBProgressHUDKey";

+ (instancetype)shareInstance{
    static MBProgressHUD *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[MBProgressHUD alloc] init];
    });
    return instance;
}

- (void)setHUD:(MBProgressHUD *)HUD{
    objc_setAssociatedObject(self, MBProgressHUDKey, HUD, OBJC_ASSOCIATION_RETAIN);
}

- (MBProgressHUD *)HUD{
    return objc_getAssociatedObject(self, MBProgressHUDKey);
}

+ (void)showTipMessage:(NSString *)message{
    [self showMessage:message InView:nil HUDType:MBProgressHUDTypeOnlyText];
    [[MBProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:1.0];
}

+ (void)showBottomMessage:(NSString *)message{
    [self showMessage:message InView:nil HUDType:MBProgressHUDTypeOnlyTextBottom];
    [[MBProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:1.0];
}

+ (void)showSuccessMessage:(NSString *)message{
    [self showMessage:message InView:nil HUDType:MBProgressHUDTypeSuccess];
    [[MBProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:1.0];
}

+ (void)showErrorMessage:(NSString *)message{
    [self showMessage:message InView:nil HUDType:MBProgressHUDTypeError];
    [[MBProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:1.0];
}

+ (void)showInfoMessage:(NSString *)message{
    [self showMessage:message InView:nil HUDType:MBProgressHUDTypeInfo];
    [[MBProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:1.0];
}

+ (void)showWarnMessage:(NSString *)message{
    [self showMessage:message InView:nil HUDType:MBProgressHUDTypeWarn];
    [[MBProgressHUD shareInstance].HUD hideAnimated:YES afterDelay:1.0];
}

+ (void)showProgressMessageInWindow:(NSString *)message{
    [self showMessage:message InView:nil HUDType:MBProgressHUDTypeLoading];
}

+ (void)showProgressMessageInView:(NSString *)message{
    [self showMessage:message InView:[self getCurrentUIVC].view HUDType:MBProgressHUDTypeLoading];
}

+ (void)hideHUD{
    if ([MBProgressHUD shareInstance].HUD != nil) {
        [[MBProgressHUD shareInstance].HUD hideAnimated:YES];
    }
}

+ (void)hideHUDNoAnimation{
    if ([MBProgressHUD shareInstance].HUD != nil) {
        [[MBProgressHUD shareInstance].HUD hideAnimated:NO];
    }
}

+ (void)showMessage:(NSString *)message InView:(UIView *)view HUDType:(MBProgressHUDType) hudType{
    
    if ([MBProgressHUD shareInstance].HUD != nil) {
        [[MBProgressHUD shareInstance].HUD hideAnimated:NO];
        [MBProgressHUD shareInstance].HUD = nil;
    }
    if ([UIScreen mainScreen].bounds.size.height == 480) {
        [view endEditing:YES];
    }
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
        //某些时候打开app显示hud，此时没有对应的view会崩溃，所以return
        if (view == nil) {
            return;
        }
    }
    [MBProgressHUD shareInstance].HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    [MBProgressHUD shareInstance].HUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
    [MBProgressHUD shareInstance].HUD.backgroundColor = [UIColor clearColor];
    [MBProgressHUD shareInstance].HUD.bezelView.color = [UIColor blackColor];
    [MBProgressHUD shareInstance].HUD.contentColor = [UIColor whiteColor];
    [[MBProgressHUD shareInstance].HUD setRemoveFromSuperViewOnHide:YES];
    [MBProgressHUD shareInstance].HUD.detailsLabel.font = [UIFont systemFontOfSize:kAutoFont(16.f)];
    if(message){
        [MBProgressHUD shareInstance].HUD.detailsLabel.text = message;
    }
    
    switch (hudType) {
            
        case MBProgressHUDTypeLoading:
            [MBProgressHUD shareInstance].HUD.mode = MBProgressHUDModeIndeterminate;
            break;
            
        case MBProgressHUDTypeOnlyText:
            [[MBProgressHUD shareInstance].HUD setMargin:15];
            [MBProgressHUD shareInstance].HUD.mode = MBProgressHUDModeText;
            break;
            
        case MBProgressHUDTypeOnlyTextBottom:
            [[MBProgressHUD shareInstance].HUD setMargin:15];
            [MBProgressHUD shareInstance].HUD.mode = MBProgressHUDModeText;
            [MBProgressHUD shareInstance].HUD.offset = CGPointMake(0.f, [UIScreen mainScreen].bounds.size.height/2 - 80);
            break;
            
        case MBProgressHUDTypeSuccess:
            [self showCustomIcon:@"MBHUD_Success"];
            break;
            
        case MBProgressHUDTypeError:
            [self showCustomIcon:@"MBHUD_Error"];
            break;
            
        case MBProgressHUDTypeInfo:
            [self showCustomIcon:@"MBHUD_Info"];
            break;
            
        case MBProgressHUDTypeWarn:
            [self showCustomIcon:@"MBHUD_Warn"];
            break;
        default:
            break;
    }
}


+ (void)showCustomIcon:(NSString *)iconName{
    [MBProgressHUD shareInstance].HUD.mode = MBProgressHUDModeCustomView;
    UIImage * hudImage = [UIImage imageNamed:[@"MBProgressHUD+DGfExtension.bundle/MBProgressHUD" stringByAppendingPathComponent:iconName]];
    UIImage * newImage = [hudImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [MBProgressHUD shareInstance].HUD.customView = [[UIImageView alloc] initWithImage:newImage];
}

+ (UIViewController *)getCurrentWindowVC{
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal){
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tempWindow in windows){
            if (tempWindow.windowLevel == UIWindowLevelNormal){
                window = tempWindow;
                break;
            }
        }
    }
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]){
        result = nextResponder;
    }
    else{
        result = window.rootViewController;
    }
    return  result;
}

+ (UIViewController *)getCurrentUIVC{
    UIViewController  *superVC = [[self class]  getCurrentWindowVC ];
    if ([superVC isKindOfClass:[UITabBarController class]]) {
        UIViewController  *tabSelectVC = ((UITabBarController*)superVC).selectedViewController;
        if ([tabSelectVC isKindOfClass:[UINavigationController class]]) {
            return ((UINavigationController*)tabSelectVC).viewControllers.lastObject;
        }
        return tabSelectVC;
    }else
        if ([superVC isKindOfClass:[UINavigationController class]]) {
            return ((UINavigationController*)superVC).viewControllers.lastObject;
        }
    return superVC;
}

#pragma mark ————— 顶部tip —————
//+ (void)showTopTipMessage:(NSString *)msg {
//    [self showTopTipMessage:msg isWindow:NO];
//}
//+ (void)showTopTipMessage:(NSString *)msg isWindow:(BOOL) isWindow{
//    
//    CGFloat padding = 10;
//    UILabel *label = [UILabel new];
//    label.text = msg;
//    label.font = [UIFont systemFontOfSize:16];
//    label.textAlignment = NSTextAlignmentCenter;
//    label.textColor = [UIColor whiteColor];
//    label.backgroundColor = [UIColor colorWithRed:0.033 green:0.685 blue:0.978 alpha:0.8];
//    label.lz_width = kScreenWidth;
////    label.textContainerInset = UIEdgeInsetsMake(padding+padding, padding, 0, padding);
//
//    if (isWindow) {
//        label.lz_height = kNaviHeight;
//        label.lz_bottom = 0;
//        [kAppWindow addSubview:label];
//
//        [UIView animateWithDuration:0.3 animations:^{
//            label.lz_top = 0;
//        } completion:^(BOOL finished) {
//            [UIView animateWithDuration:0.2 delay:2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//                label.lz_bottom = 0;
//            } completion:^(BOOL finished) {
//                [label removeFromSuperview];
//            }];
//        }];
//
//    }else{
//        label.lz_height = [msg heightForFont:label.font width:label.lz_width] + 2 * padding;
//        label.bottom = (kiOS7Later ? 64 : 0);
//        [[kAppDelegate getCurrentUIVC].view addSubview:label];
//
//        [UIView animateWithDuration:0.3 animations:^{
//            label.top = (kiOS7Later ? 64 : 0);
//        } completion:^(BOOL finished) {
//            [UIView animateWithDuration:0.2 delay:2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//                label.bottom = (kiOS7Later ? 64 : 0);
//            } completion:^(BOOL finished) {
//                [label removeFromSuperview];
//            }];
//        }];
//
//    }
//
//}

@end
